package com.student.view;

import com.student.utils.DBMysql;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

public class rewardView extends JFrame {

    public rewardView() {
        super("学生奖惩信息管理系统");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5);

        // 学号框和搜索按钮
        gbc.gridy = 0;
        gbc.gridx = 0;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.LINE_START;
        add(new JLabel("学号:"), gbc);

        gbc.gridx = 1;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.LINE_START;
        JTextField studentIDTextField = new JTextField(20);
        add(studentIDTextField, gbc);

        gbc.gridx = 3;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.LINE_START;
        JButton searchButton = new JButton("搜索");
        add(searchButton, gbc);

/**
 * 一等奖学金
 */
        gbc.gridy = 1;
        gbc.gridx = 0;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.LINE_START;
        add(new JLabel("一等奖学金"), gbc);

        gbc.gridx = 1;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.LINE_START;
        JTextField firstPrizeTextField = new JTextField(20);
        add(firstPrizeTextField, gbc);

        gbc.gridx = 2;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.LINE_START;
        JButton firstPrizeAddButton = new JButton("添加");
        firstPrizeAddButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 一等奖学金添加逻辑
                String studentID = studentIDTextField.getText().trim();
                if (studentID.isEmpty()) {
                    JOptionPane.showMessageDialog(rewardView.this, "请输入学号", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }else {
                  DBMysql.update("update  s_rewards set first_class_award = first_class_award+1 where number=? ", studentID);
                }
            }
        });
        add(firstPrizeAddButton, gbc);

        gbc.gridx = 3;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.LINE_START;
        JButton firstPrizeDeleteButton = new JButton("删除");
        firstPrizeDeleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 一等奖学金删除逻辑
                String studentID = studentIDTextField.getText().trim();
                if (studentID.isEmpty()) {
                    JOptionPane.showMessageDialog(rewardView.this, "请输入学号", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }else {
                    //增加不能小于0逻辑
                    ResultSet rs = DBMysql.query("select * from s_rewards where number=? ", studentID);
                    try {
                        if(rs.getInt("first_class_award")==0){
                            JOptionPane.showMessageDialog(rewardView.this, "删除失败", "错误", JOptionPane.ERROR_MESSAGE);
                        }else{
                            DBMysql.update("update  s_rewards set first_class_award = first_class_award-1 where number=? ", studentID);
                        }
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }

                }
            }
        });
        add(firstPrizeDeleteButton, gbc);

/**
 * 二等奖学金
 */
        gbc.gridy = 2;
        gbc.gridx = 0;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.LINE_START;
        add(new JLabel("二等奖学金"), gbc);

        gbc.gridx = 1;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.LINE_START;
        JTextField secondPrizeTextField = new JTextField(20);
        add(secondPrizeTextField, gbc);

        gbc.gridx = 2;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.LINE_START;
        JButton secondPrizeAddButton = new JButton("添加");
        secondPrizeAddButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 二等奖学金添加逻辑
                String studentID = studentIDTextField.getText().trim();
                if (studentID.isEmpty()) {
                    JOptionPane.showMessageDialog(rewardView.this, "请输入学号", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }else {
                    DBMysql.update("update  s_rewards set second_class_award = second_class_award+1 where number=? ", studentID);
                }
            }
        });
        add(secondPrizeAddButton, gbc);

        gbc.gridx = 3;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.LINE_START;
        JButton secondPrizeDeleteButton = new JButton("删除");
        secondPrizeDeleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String studentID = studentIDTextField.getText().trim();
                if (studentID.isEmpty()) {
                    JOptionPane.showMessageDialog(rewardView.this, "请输入学号", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }else {
                    //增加不能小于0逻辑
                    ResultSet rs = DBMysql.query("select * from s_rewards where number=? ", studentID);
                    try {
                        if(rs.getInt("second_class_award")==0){
                            JOptionPane.showMessageDialog(rewardView.this, "删除失败", "错误", JOptionPane.ERROR_MESSAGE);
                        }else{
                            DBMysql.update("update  s_rewards set second_class_award = second_class_award-1 where number=? ", studentID);
                        }
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }

                }

            }
        });
        add(secondPrizeDeleteButton, gbc);

/**
 * 旷课
 */
        gbc.gridy = 3;
        gbc.gridx = 0;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.LINE_START;
        add(new JLabel("旷课"), gbc);

        gbc.gridx = 1;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.LINE_START;
        JTextField absenceTextField = new JTextField(20);
        add(absenceTextField, gbc);

        gbc.gridx = 2;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.LINE_START;
        JButton absenceAddButton = new JButton("添加");
        absenceAddButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 旷课添加逻辑
                String studentID = studentIDTextField.getText().trim();
                if (studentID.isEmpty()) {
                    JOptionPane.showMessageDialog(rewardView.this, "请输入学号", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }else {
                    DBMysql.update("update  s_rewards set Absence = Absence+1 where number=? ", studentID);
                }
            }
        });
        add(absenceAddButton, gbc);

        gbc.gridx = 3;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.LINE_START;
        JButton absenceDeleteButton = new JButton("删除");
        absenceDeleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 旷课删除逻辑
                String studentID = studentIDTextField.getText().trim();
                if (studentID.isEmpty()) {
                    JOptionPane.showMessageDialog(rewardView.this, "请输入学号", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }else {
                    //增加不能小于0逻辑
                    ResultSet rs = DBMysql.query("select * from s_rewards where number=? ", studentID);
                    try {
                        if(rs.getInt("Absence")==0){
                            JOptionPane.showMessageDialog(rewardView.this, "删除失败", "错误", JOptionPane.ERROR_MESSAGE);
                        }else{
                            DBMysql.update("update  s_rewards set Absence = Absence-1 where number=? ", studentID);
                        }
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }

                }
            }
        });
        add(absenceDeleteButton, gbc);

/**
 * 迟到
 */
        gbc.gridy = 4;
        gbc.gridx = 0;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.LINE_START;
        add(new JLabel("迟到"), gbc);

        gbc.gridx = 1;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.LINE_START;
        JTextField lateTextField = new JTextField(20);
        add(lateTextField, gbc);

        gbc.gridx = 2;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.LINE_START;
        JButton lateAddButton = new JButton("添加");
        lateAddButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 迟到添加逻辑
                String studentID = studentIDTextField.getText().trim();
                if (studentID.isEmpty()) {
                    JOptionPane.showMessageDialog(rewardView.this, "请输入学号", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }else {
                    DBMysql.update("update  s_rewards set be_late = be_late +1 where number=? ", studentID);
                }
            }
        });
        add(lateAddButton, gbc);

        gbc.gridx = 3;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.LINE_START;
        JButton lateDeleteButton = new JButton("删除");
        lateDeleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 迟到删除逻辑
                String studentID = studentIDTextField.getText().trim();
                if (studentID.isEmpty()) {
                    JOptionPane.showMessageDialog(rewardView.this, "请输入学号", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }else {
                    //增加不能小于0逻辑
                    ResultSet rs = DBMysql.query("select * from s_rewards where number=? ", studentID);
                    try {
                        if(rs.getInt("be_late")==0){
                            JOptionPane.showMessageDialog(rewardView.this, "删除失败", "错误", JOptionPane.ERROR_MESSAGE);
                        }else{
                            DBMysql.update("update  s_rewards set be_late = be_late-1 where number=? ", studentID);
                        }
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }

                }
            }
        });
        add(lateDeleteButton, gbc);

        /**
         * 搜索
         */
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 搜索学生信息的逻辑
                String studentID = studentIDTextField.getText().trim();

                // 检查输入是否有效
                if (studentID.isEmpty()) {
                    JOptionPane.showMessageDialog(rewardView.this, "请输入学号", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }else {
                    ResultSet rs = DBMysql.query("select * from s_rewards where number=? ", studentID);
                    try {
                        String x1 = rs.getString("first_class_award");
                        String x2 = rs.getString("second_class_award");
                        String absence1 = rs.getString("Absence");
                        String late1 = rs.getString("be_late");
                        String number1 = rs.getString("number");
                        secondPrizeTextField.setText(x2);
                        firstPrizeTextField.setText(x1);
                        absenceTextField.setText(absence1);
                        lateTextField.setText(late1);

                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }



                }
            }
        });

        setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            new rewardView();
        });
    }
}
